#include <vector>
#include <mutex>
#include <condition_variable>
#include <semaphore.h>
using namespace std;

template<class T>
class RingQueue
{
public:
    mutex c_mutex;
    mutex p_mutex;
    condition_variable c_con;
    condition_variable p_con;
    int data_cont;
    int space_cont;

    RingQueue(size_t cap = 5)
    :_queue(cap)
    ,_cap(cap)
    ,c_pos(0)
    ,p_pos(0)
    ,data_cont(0)
    ,space_cont(cap)
    {
    }
    ~RingQueue()
    {

    }

    void push(T& data)
    {
        _queue[p_pos] = data;
        p_pos++;
        p_pos %= _cap;
        data_cont++;
        space_cont--;
    }

    T& pop()
    {
        data_cont--;
        space_cont++;
        T& ret = _queue[c_pos];
        c_pos++;
        c_pos %= _cap;
        return ret;
    }

    bool isFull()
    {
        return data_cont == _cap;
    }

    bool isEmpty()
    {
        return space_cont == _cap;
    }

private:
    vector<T> _queue;//数组模拟环形队列
    size_t _cap;
    int c_pos;
    int p_pos; 
};